/*
 * Author Josinho Joseph J
 */
package com.sdi.columbus;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_screen);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		new Handler().postDelayed(new Runnable() {
			
			public void run() {
				
				new performBackgroundTask().execute();
				
			}
		}, 1500);
	}
	
	//checks the network availability
	public boolean isNetworkAvailable() {
	    ConnectivityManager cm = (ConnectivityManager) 
	    	getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
	    // if no network is available networkInfo will be null
	    // otherwise check if we are connected
	    if (networkInfo != null && networkInfo.isConnected()) {
	        return true;
	    }
	    return false;
	}
	
	//class to perform execute tasks in background
	private class performBackgroundTask extends AsyncTask < Void, Void, Void > 
    {
		
       private ProgressDialog pdia;

       @Override
       protected void onPreExecute(){
          super.onPreExecute();
               pdia = new ProgressDialog(SplashScreen.this);
               pdia.setMessage("Checking Internet....");
               pdia.show();        
       }
 
       @Override
       protected Void doInBackground(Void... params) {
    	   
    	   Intent intent;
    	   boolean stat=isNetworkAvailable();
    	   if(stat)
    	   {
    		   intent=new Intent(SplashScreen.this,LoginActivity.class);
    		   startActivity(intent);
    		   SplashScreen.this.finish();
    	   }
    	   else
    	   {
    		   intent =new Intent(SplashScreen.this,InternetStatus.class);
    		   startActivity(intent);
    		   SplashScreen.this.finish();
    	   }
    	   return null;
       }

       @Override
       protected void onPostExecute(Void result) {
    	   super.onPostExecute(result);
    	   pdia.dismiss();
       }

       @Override
       protected void onProgressUpdate(Void... values) {
    	   super.onProgressUpdate(values);
       }

    }

}
